package com.ruoyi.celoan.handanUtils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.SM2;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.celoan.handanUtils.request.HanDanDataRequest;
import com.ruoyi.celoan.handanUtils.response.CountryPlatformResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class HanDanHttpUtils {
    private static final Logger log = LoggerFactory.getLogger(HanDanHttpUtils.class);
    public static final String myPublicKey = "049ecc3b71e8e43ae240298f9f55de4b9ad3733236ad5a3782448fe8a7d0a22f8b116c98dd9904871037d20b131b1b4d8165ef534f25205d628c5474448b5fc15e";
    public static final String myPrivateKey = "00969996be634b00d61a465f27403d0e7346d9d930d008d0f19f840604e89592dc";
    private static String app_id = "iv63koa14o06lw74";
    public static String apiUrl = "http://10.242.254.70:28080";
    private static String publicKey = null;

    public static String publicKey() {
        if (StringUtils.isEmpty(publicKey)) {
            log.info("开始获取秘钥");
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> txnBodyCom = new HashMap<>();
            txnBodyCom.put("appId", app_id);
            txnBodyCom.put("publicKey", myPublicKey);
            map.put("txnBodyCom", txnBodyCom);
            Map<String, Object> txnCommCom = new HashMap<>();
            txnCommCom.put("txnIttChnlId", "99990001000000000000000");
            txnCommCom.put("txnIttChnlCgyCode", "99999999");
            map.put("txnCommCom", txnCommCom);
            String url = "/api/publicKey";
            log.info("请求参数:" + JSONObject.toJSONString(map));
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(type);
            headers.add("Accept", "application/json;charset=UTF-8");
            headers.set("Referer", apiUrl);
            HttpEntity<String> requestEntity = new HttpEntity(JSONObject.toJSONString(map), (MultiValueMap) headers);
            restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
            ResponseEntity<String> response = restTemplate.postForEntity(apiUrl + url, requestEntity, String.class, new Object[0]);
            String responseStr = (String) response.getBody();
            log.info("返回结果:" + responseStr);
            JSONObject jsonObject = JSONObject.parseObject(responseStr);
            String result = jsonObject.getString("C-API-Status");
            CountryPlatformResponse countryPlatformResponse = (CountryPlatformResponse) jsonObject.getObject("C-Response-Body", CountryPlatformResponse.class);
            if (!ObjectUtil.isNotNull(countryPlatformResponse)) {
                countryPlatformResponse = new CountryPlatformResponse();
            }
            if (StringUtils.equals("00", result)) {
                countryPlatformResponse.setApiStatus(true);
            } else {
                countryPlatformResponse.setApiStatus(false);
            }
            SM2 sm2 = SmUtil.sm2(myPrivateKey, myPublicKey);
            String responseData = HanDanBean.dealResult(sm2, countryPlatformResponse);
            JSONObject jsonObject1 = JSONObject.parseObject(responseData);
            log.info("解析后结果：" + JSONObject.toJSONString(jsonObject1));
            publicKey = jsonObject1.getString("publicKey");
        }
        return publicKey;
    }

    public CountryPlatformResponse updateUser(String url, String json) {
        return null;
    }

    public static String getToken() {
        String url = "/api/getToken";
        BasePlatformRequest request = HanDanBean.jiaMiToken();
        Map<String, Object> map = new HashMap<>();
        map.put("txnBodyCom", request);
        Map<String, Object> txnCommCom = new HashMap<>();
        txnCommCom.put("txnIttChnlId", "99990001000000000000000");
        txnCommCom.put("txnIttChnlCgyCode", "99999999");
        map.put("txnCommCom", txnCommCom);
        return postGetToken(url, JSONObject.toJSONString(map));
    }

    public static CountryPlatformResponse uploadEnterprises(String url, String json, String token) {
        HanDanDataRequest request = HanDanBean.jiaMiPort(json, token);
        return post(url, request);
    }

    public static CountryPlatformResponse uploadLoginLogs(String url, String json, String token) {
        HanDanDataRequest request = HanDanBean.jiaMiPort(json, token);
        return post(url, request);
    }

    public static CountryPlatformResponse financingRequirements(String url, String json, String token) {
        HanDanDataRequest request = HanDanBean.jiaMiPort(json, token);
        return post(url, request);
    }

    public static CountryPlatformResponse creditLending(String url, String json, String token) {
        HanDanDataRequest request = HanDanBean.jiaMiPort(json, token);
        return post(url, request);
    }

    public static CountryPlatformResponse financialInstitutions(String url, String json, String token) {
        HanDanDataRequest request = HanDanBean.jiaMiPort(json, token);
        return post(url, request);
    }

    public static CountryPlatformResponse financialProducts(String url, String json, String token) {
        HanDanDataRequest request = HanDanBean.jiaMiPort(json, token);
        return post(url, request);
    }

    public static CountryPlatformResponse policies(String url, String json, String token) {
        HanDanDataRequest request = HanDanBean.jiaMiPort(json, token);
        return post(url, request);
    }

    public static CountryPlatformResponse creditServiceAgencies(String url, String json, String token) {
        HanDanDataRequest request = HanDanBean.jiaMiPort(json, token);
        return post(url, request);
    }

    public static CountryPlatformResponse creditStatistics(String url, String json, String token) {
        HanDanDataRequest request = HanDanBean.jiaMiPort(json, token);
        return post(url, request);
    }

    public static CountryPlatformResponse post(String url, HanDanDataRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("txnBodyCom", request);
        Map<String, Object> txnCommCom = new HashMap<>();
        txnCommCom.put("txnIttChnlId", "99990001000000000000000");
        txnCommCom.put("txnIttChnlCgyCode", "99999999");
        map.put("txnCommCom", txnCommCom);
        map.put("Referer", apiUrl);
        log.info("请求参数:" + JSONObject.toJSONString(map));
        log.info("请求接口:" + apiUrl + url);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", "application/json;charset=UTF-8");
        headers.set("Referer", apiUrl);
        HttpEntity<String> requestEntity = new HttpEntity(JSONObject.toJSONString(map), (MultiValueMap) headers);
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        ResponseEntity<String> response = restTemplate.postForEntity(apiUrl + url, requestEntity, String.class, new Object[0]);
        String responseStr = (String) response.getBody();
        log.info("返回结果:" + responseStr);
        JSONObject jsonObject = JSONObject.parseObject(responseStr);
        String result = jsonObject.getString("C-API-Status");
        CountryPlatformResponse countryPlatformResponse = (CountryPlatformResponse) jsonObject.getObject("C-Response-Body", CountryPlatformResponse.class);
        if (!ObjectUtil.isNotNull(countryPlatformResponse)) {
            countryPlatformResponse = new CountryPlatformResponse();
        }
        if (StringUtils.equals("00", result)) {
            countryPlatformResponse.setApiStatus(true);
        } else {
            countryPlatformResponse.setApiStatus(false);
        }
        return countryPlatformResponse;
    }

    public static String postGetToken(String url, String json) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", "application/json;charset=UTF-8");
        headers.set("Referer", apiUrl);
        HttpEntity<String> requestEntity = new HttpEntity(json, (MultiValueMap) headers);
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        ResponseEntity<String> response = restTemplate.postForEntity(apiUrl + url, requestEntity, String.class, new Object[0]);
        String responseStr = (String) response.getBody();
        log.info("返回结果" + responseStr);
        JSONObject jsonObject = JSONObject.parseObject(responseStr);
        String result = jsonObject.getString("C-API-Status");
        CountryPlatformResponse countryPlatformResponse = (CountryPlatformResponse) jsonObject.getObject("C-Response-Body", CountryPlatformResponse.class);
        if (!ObjectUtil.isNotNull(countryPlatformResponse)) {
            countryPlatformResponse = new CountryPlatformResponse();
        }
        if (StringUtils.equals("00", result)) {
            countryPlatformResponse.setApiStatus(true);
        } else {
            countryPlatformResponse.setApiStatus(false);
        }
        SM2 sm2 = SmUtil.sm2(myPrivateKey, myPublicKey);
        String responseData = HanDanBean.dealResult(sm2, countryPlatformResponse);
        JSONObject jsonObject1 = JSONObject.parseObject(responseData);
        log.info("token:::" + jsonObject1.getString("token"));
        return jsonObject1.getString("token");
    }
}


/* Location:              D:\tuodayun\workspace\work\celoan-third-data-report-service-2.0.0-SNAPSHOT.jar!\BOOT-INF\classes\com\celoan\handanUtils\HanDanHttpUtils.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */
